package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.danmo.domain.DanmoFontGenerate;
import com.muyu.danmo.req.DanmoFamousCardReq;
import com.muyu.danmo.req.DanmoFontSingleReq;
import com.muyu.danmo.req.DanmoFontTypeReq;
import com.muyu.danmo.service.IDanmoFontGenerateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 字体信息Controller
 *
 * @author yangle
 * @date 2024-09-14
 */
@RestController
@RequestMapping("/generate")
public class DanmoFontGenerateController extends BaseController
{
    @Autowired
    private IDanmoFontGenerateService danmoFontGenerateService;

    /**
     * 查询字体信息列表
     */
//    @RequiresPermissions("system:generate:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoFontGenerate danmoFontGenerate)
    {
        PageHelper.startPage(danmoFontGenerate.getPageNum(),danmoFontGenerate.getPageSize());
        List<DanmoFontGenerate> list = danmoFontGenerateService.selectDanmoFontGenerateList(danmoFontGenerate);
        PageInfo<DanmoFontGenerate> danmoFontGeneratePageInfo = new PageInfo<>(list);
        return Result.success(danmoFontGeneratePageInfo);
    }

    /**
     * 导出字体信息列表
     */
    @RequiresPermissions("system:generate:export")
    @Log(title = "字体信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoFontGenerate danmoFontGenerate)
    {
        List<DanmoFontGenerate> list = danmoFontGenerateService.selectDanmoFontGenerateList(danmoFontGenerate);
        ExcelUtil<DanmoFontGenerate> util = new ExcelUtil<DanmoFontGenerate>(DanmoFontGenerate.class);
        util.exportExcel(response, list, "字体信息数据");
    }

    /**
     * 获取字体信息详细信息
     */
//    @RequiresPermissions("system:generate:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(danmoFontGenerateService.selectDanmoFontGenerateById(id));
    }

    /**
     * 新增字体信息
     */
//    @RequiresPermissions("system:generate:add")
    @Log(title = "字体信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoFontGenerate danmoFontGenerate)
    {
        return toAjax(danmoFontGenerateService.insertDanmoFontGenerate(danmoFontGenerate));
    }

    /**
     * 修改字体信息
     */
//    @RequiresPermissions("system:generate:edit")
    @Log(title = "字体信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoFontGenerate danmoFontGenerate)
    {
        return toAjax(danmoFontGenerateService.updateDanmoFontGenerate(danmoFontGenerate));
    }

    /**
     * 删除字体信息
     */
//    @RequiresPermissions("system:generate:remove")
    @Log(title = "字体信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(danmoFontGenerateService.deleteDanmoFontGenerateByIds(ids));
    }

    /**
     * 生成单个字体
     * @param danmoFontSingleReq
     * @return
     */
     @PostMapping   ("/fontSingle")
    public Result generate(@RequestBody DanmoFontSingleReq danmoFontSingleReq) throws IOException {
         //开始时间
         long startTime = System.currentTimeMillis();
         //结束时间
         long endTime = System.currentTimeMillis();
         long time = endTime - startTime;
         long time2 = time / 1000;
         System.out.println("生成单个字体耗时：" + time2 + "秒");
         if (danmoFontSingleReq.getFont()==null || danmoFontSingleReq.getFont().size()==0){
             return Result.error("请输入文字");
         }
         return  danmoFontGenerateService.generate(danmoFontSingleReq);
     }

    /**
     * 查询某一个字体
     *
     * @param danmoFontSingleReq
     * @return
     */
     @PostMapping("/queryFont")
     public Result<PageInfo<DanmoFontGenerate>> queryFont(@RequestBody DanmoFontSingleReq danmoFontSingleReq)
     {
          //开始时间
         long startTime = System.currentTimeMillis();
         PageHelper.startPage(danmoFontSingleReq.getPageNum(), danmoFontSingleReq.getPageSize());
         List<DanmoFontGenerate> danmoFontGenerates = danmoFontGenerateService.queryFont(danmoFontSingleReq);

         PageInfo<DanmoFontGenerate> danmoFontGeneratePageInfo = new PageInfo<>(danmoFontGenerates);
         //结束时间
         long endTime = System.currentTimeMillis();
         long time = endTime - startTime;
         long time2 = time / 1000;
         System.out.println("生成单个字体耗时：" + time2 + "秒");
         return Result.success(danmoFontGeneratePageInfo);
     }

    /**
     * 查询字体类型
     * @param danmoFontTypeReq
     * @return
     */
     @PostMapping("/selectFontType")
    public Result<List<DanmoFontGenerate>> selectFontType(@RequestBody DanmoFontTypeReq danmoFontTypeReq)
    {
        return Result.success(danmoFontGenerateService.selectFontType(danmoFontTypeReq));
    }

    /**
     * 查询字贴浏览
     */
    @PostMapping("/queryFamousCard")
    public Result queryFontView(@RequestBody DanmoFamousCardReq danmoFamousCardReq)
    {
        List<DanmoFontGenerate> list =danmoFontGenerateService.queryFamousCard(danmoFamousCardReq);
        return Result.success(list);
    }


}
